﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MOJsdk;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_GeneralSettings.xaml
    /// </summary>
    public partial class Control_GeneralSettings : UserControl
    {
        public Control_GeneralSettings()
        {
            InitializeComponent();
            INI();
        }

        private void INI()
        {
            LoadControls();
            INIStyles();

            this.Loaded += delegate { LoadControls(); };
            _btnSave.Click += delegate { SaveData(); };
            _btn_installStyle.Click += InstallStyle;
        }
        private void INIStyles()
        {
            _lblLang.Content = MOJLocalization.Localize("Language") + ": ";
            _lblStyle.Content = MOJLocalization.Localize("Style") + ": ";
            _btnSave.Content = MOJLocalization.Localize("Save");
            _btn_installStyle.Content = MOJLocalization.Localize("Install Style");

            _lblLang.ApplyStyle("0101061");
            _lblStyle.ApplyStyle("0101062");
            _cmbLang.ApplyStyle("0101063");
            _cmbStyles.ApplyStyle("0101064");
            _btnSave.ApplyStyle("0101065");
            _btn_installStyle.ApplyStyle("0101105");
        }
        private void LoadControls()
        {
            _cmbStyles.Items.Clear();
            _cmbLang.Items.Clear();

            #region LoadLangs
            _cmbLang.Items.Add("Default");
            List<string> folders = new List<string>(System.IO.Directory.EnumerateFiles(GlobalVars.TheHost.SystemVars.LanguageFolder, "*.xml", System.IO.SearchOption.TopDirectoryOnly));
            foreach (var v in folders)
                _cmbLang.Items.Add(System.IO.Path.GetFileNameWithoutExtension(v));
            for (int i = 0; i < _cmbLang.Items.Count; i++)
            {
                if (System.IO.Path.GetFileNameWithoutExtension(_cmbLang.Items[i].ToString().ToLower()) == System.IO.Path.GetFileNameWithoutExtension(GlobalVars.TheHost.SystemVars.LanguageFile.ToLower()))
                    _cmbLang.SelectedIndex = i;
            }
            #endregion

            #region LoadStyles
            foreach (var v in GlobalVars.TheHost.GetStyles())
                _cmbStyles.Items.Add(v);

            if (_cmbStyles.Items.Count > 0)
            {
                _cmbStyles.IsEnabled = true;

                if (GlobalVars.TheHost.HostStyle != null)
                    for (int i = 0; i < _cmbStyles.Items.Count; i++)
                        if (((IStyle)_cmbStyles.Items[i]).StyleName == GlobalVars.TheHost.HostStyle.StyleName)
                            _cmbStyles.SelectedIndex = i;
            }
            else
            {
                _cmbStyles.IsEnabled = false;
            }
            #endregion
        }
        private void InstallStyle(object sender, RoutedEventArgs e)
        {
            GlobalVars.TheHost.InstallStyle();
            LoadControls();
        }

        private void SaveData()
        {
            string lang = _cmbLang.SelectedItem.ToString();
            if (lang.IsNullorEmpty() || lang.ToLower() == "default")
                lang = "DEFAULT";
            else
            {
                lang = System.IO.Path.Combine(GlobalVars.TheHost.SystemVars.LanguageFolder, lang + ".xml");
            }
            GlobalVars.TheHost.SystemVars.SetVar("MOJLanguage", lang);


            string style = _cmbStyles.SelectedIndex == -1 ? "NULL" : ((IStyle)_cmbStyles.SelectedItem).StyleName;
            GlobalVars.TheHost.SystemVars.SetVar("MOJSkin", style);
        }
    }
}
